﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class RuleDetailChannelDAL
    {
        private static RuleDetailChannel CreateRuleDetailChannelFromReader(SqlDataReader reader)
        {
            RuleDetailChannel ruleDetailChannel = new RuleDetailChannel
            {
                ChannelID = Utility.StringParse(reader[RuleDetailChannelColumns.ChannelID.ToString()]),
                No = Utility.StringParse(reader[RuleDetailChannelColumns.No.ToString()]),
                RuleDetailID = Utility.StringParse(reader[RuleDetailChannelColumns.RuleDetailID.ToString()]),
                RuleID = Utility.StringParse(reader[RuleDetailChannelColumns.RuleID.ToString()])
            };

            return ruleDetailChannel;
        }

        public static List<RuleDetailChannel> GetRuleDetailChannelList(string ruleDetailID)
        {
            try
            {
                List<RuleDetailChannel> ruleDetailChannelList = new List<RuleDetailChannel>();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.NVarChar).Value = ruleDetailID;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailChannelGetList))
                {
                    while (reader.Read())
                    {
                        ruleDetailChannelList.Add(CreateRuleDetailChannelFromReader(reader));
                    }
                }

                return ruleDetailChannelList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailChannelList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILCHANNEL_GETLIST);
            }
        }

        public static bool DeleteRuleDetailChannel(string ruleID)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailChannelDelete) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILCHANNEL_DELETE);
            }
            catch (SqlException ex)
            {
                Log.LogEx("DeleteRuleDetailChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILCHANNEL_DELETE);
            }
        }

        public bool InsertRuleDetailChannel(RuleDetailChannel ruleDetailChannel)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("No", SqlDbType.NVarChar).Value = ruleDetailChannel.No;
                DataAccessHelper.SqlCmd.Parameters.Add("ChannelID", SqlDbType.NVarChar).Value = ruleDetailChannel.ChannelID;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.NVarChar).Value = ruleDetailChannel.RuleDetailID;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleDetailChannel.RuleID;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailChannelInsert) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILCHANNEL_INSERT);
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertRuleDetailChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILCHANNEL_INSERT);
            }
        }
    }
}